/*
 * ConcesionarioView.java
 */

package concesionario;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class ConcesionarioView extends FrameView {

    public ConcesionarioView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = ConcesionarioApp.getApplication().getMainFrame();
            aboutBox = new ConcesionarioAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        ConcesionarioApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu Archivo = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        Vendedores = new javax.swing.JMenu();
        AltaVendedor = new javax.swing.JMenuItem();
        BajaVendedor = new javax.swing.JMenuItem();
        ModificarVendedor = new javax.swing.JMenuItem();
        Exposiciones = new javax.swing.JMenu();
        Coches = new javax.swing.JMenu();
        Reparacion = new javax.swing.JMenu();
        ConsultaRep = new javax.swing.JMenuItem();
        EnviarARep = new javax.swing.JMenuItem();
        Clientes = new javax.swing.JMenu();
        javax.swing.JMenu Ayuda = new javax.swing.JMenu();
        javax.swing.JMenuItem AcercaDe = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        mainPanel.setName("mainPanel"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 739, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 250, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(concesionario.ConcesionarioApp.class).getContext().getResourceMap(ConcesionarioView.class);
        Archivo.setText(resourceMap.getString("Archivo.text")); // NOI18N
        Archivo.setName("Archivo"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(concesionario.ConcesionarioApp.class).getContext().getActionMap(ConcesionarioView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        Archivo.add(exitMenuItem);

        menuBar.add(Archivo);

        Vendedores.setText(resourceMap.getString("Vendedores.text")); // NOI18N
        Vendedores.setName("Vendedores"); // NOI18N

        AltaVendedor.setText(resourceMap.getString("AltaVendedor.text")); // NOI18N
        AltaVendedor.setName("AltaVendedor"); // NOI18N
        Vendedores.add(AltaVendedor);

        BajaVendedor.setText(resourceMap.getString("BajaVendedor.text")); // NOI18N
        BajaVendedor.setName("BajaVendedor"); // NOI18N
        Vendedores.add(BajaVendedor);

        ModificarVendedor.setText(resourceMap.getString("ModificarVendedor.text")); // NOI18N
        ModificarVendedor.setName("ModificarVendedor"); // NOI18N
        Vendedores.add(ModificarVendedor);

        menuBar.add(Vendedores);

        Exposiciones.setText(resourceMap.getString("Exposiciones.text")); // NOI18N
        Exposiciones.setName("Exposiciones"); // NOI18N
        menuBar.add(Exposiciones);

        Coches.setText(resourceMap.getString("Coches.text")); // NOI18N
        Coches.setName("Coches"); // NOI18N

        Reparacion.setText(resourceMap.getString("Reparacion.text")); // NOI18N
        Reparacion.setName("Reparacion"); // NOI18N

        ConsultaRep.setText(resourceMap.getString("ConsultaRep.text")); // NOI18N
        ConsultaRep.setName("ConsultaRep"); // NOI18N
        Reparacion.add(ConsultaRep);

        EnviarARep.setText(resourceMap.getString("EnviarARep.text")); // NOI18N
        EnviarARep.setName("EnviarARep"); // NOI18N
        Reparacion.add(EnviarARep);

        Coches.add(Reparacion);

        menuBar.add(Coches);

        Clientes.setText(resourceMap.getString("Clientes.text")); // NOI18N
        Clientes.setName("Clientes"); // NOI18N
        menuBar.add(Clientes);

        Ayuda.setText(resourceMap.getString("Ayuda.text")); // NOI18N
        Ayuda.setName("Ayuda"); // NOI18N

        AcercaDe.setAction(actionMap.get("showAboutBox")); // NOI18N
        AcercaDe.setText(resourceMap.getString("AcercaDe.text")); // NOI18N
        AcercaDe.setName("AcercaDe"); // NOI18N
        Ayuda.add(AcercaDe);

        menuBar.add(Ayuda);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 743, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 573, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem AltaVendedor;
    private javax.swing.JMenuItem BajaVendedor;
    private javax.swing.JMenu Clientes;
    private javax.swing.JMenu Coches;
    private javax.swing.JMenuItem ConsultaRep;
    private javax.swing.JMenuItem EnviarARep;
    private javax.swing.JMenu Exposiciones;
    private javax.swing.JMenuItem ModificarVendedor;
    private javax.swing.JMenu Reparacion;
    private javax.swing.JMenu Vendedores;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
